﻿using System;
using System.ComponentModel.Composition;
using System.Windows;
using MediaAssistant.Constants;
using MediaAssistant.Controls.MovieMiddlePanel;
using MediaAssistant.Controls.MovieSearchControl;
using MediaAssistant.Controls.MusicMiddlePanel;
using MediaAssistant.Controls.MusicSearchControl;
using MediaAssistant.DAL.Constants;
using MediaAssistant.Data;
using MediaAssistant.Helper;
using MefBasic;
using MefBasic.Commans;
using MefBasic.Data;
using Microsoft.Practices.Composite.Regions;

namespace MediaAssistant.Management
{
    [Export]
    public class LibraryManager:DependencyNotifyObject
    {
        public LibraryManager()
        {
            ChangeViewCommand = new DelegateCommand(ExecuteChangeView);
            PlayMusicCommand = new DelegateCommand(ExecutePlayMusic, CanExecutePlayMusic);
        }

        public DelegateCommand PlayMusicCommand { get; set; }

        private bool CanExecutePlayMusic(object arg)
        {
            return DataSource.Value.SelectedMusic != null;
        }

        private void ExecutePlayMusic(object obj)
        {
            DataSource.Value.Play(DataSource.Value.SelectedMusic);
        }

        public DelegateCommand ChangeViewCommand { get; set; }

        private void ExecuteChangeView(object obj)
        {
            CurrentViewType = (string) obj;
        }

        [Import]
        public WaitScreenService WaitScreenService { get; set; }

        [Import]
        private IRegionManager RegionManager { get; set; }
        [Import]
        private Lazy<MovieMiddlePanelPresenter> MovieMiddlePanelPresenter { get; set; }
        [Import]
        private Lazy<MovieSearchControlPresenter> MovieSearchControlPresenter { get; set; }
        [Import]
        private Lazy<MusicMiddlePanelPresenter> MusicMiddlePanelPresenter { get; set; }
        [Import]
        private Lazy<MusicSearchControlPresenter> MusicSearchControlPresenter { get; set; }

        [Import]
        private Lazy<LibraryDataSource> DataSource { get; set; }

        public void ActivateLastResultTypeViews(string type)
        {
            if (type == LibraryItemType.MusicType)
                ActivateMusicPanel();
            else
            {
                ActivateMoviePanel();
            }
            RegistryHelper.LastResultType = type;
            CurrentViewType = type;
        }

        private void ActivateMoviePanel()
        {
            RegionManager.Regions[RegionNames.MiddleRegion].Activate(MovieMiddlePanelPresenter.Value.View);
            RegionManager.Regions[MediaAssistantRegionNames.SearchRegion].Activate(MovieSearchControlPresenter.Value.View);

        }

        private void ActivateMusicPanel()
        {
            RegionManager.Regions[RegionNames.MiddleRegion].Activate(MusicMiddlePanelPresenter.Value.View);
            RegionManager.Regions[MediaAssistantRegionNames.SearchRegion].Activate(MusicSearchControlPresenter.Value.View);
        }

        public string CurrentViewType
        {
            get { return (string)GetValue(CurrentViewTypeProperty); }
            set { SetValue(CurrentViewTypeProperty, value); }
        }

        public static readonly DependencyProperty CurrentViewTypeProperty =
            DependencyProperty.Register("CurrentViewType", typeof(string), typeof(LibraryManager), new UIPropertyMetadata(RegistryHelper.LastResultType, OnPropertyChanged));

        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var manager = (LibraryManager)d;
            manager.DoPropertyChanged(e);
        }

        protected void DoPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            switch (e.Property.Name)
            {
                case "CurrentViewType":
                    ActivateLastResultTypeViews(CurrentViewType);
                    break;
            }
        }

        public void RaiseCanExecuteChanged()
        {
            PlayMusicCommand.RaiseCanExecuteChanged();
            ChangeViewCommand.RaiseCanExecuteChanged();
        }
    }
}
